<?php
/**
 * Author: Traveler
 * Time  : 2021/10/15 12:08
 */

namespace app\service;


use app\model\IcafeCnkModel;
use sunshine\basic\RestService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class IcafeCnkService extends RestService
{
    /**
     * IcafeCnkService constructor.
     */
    public function __construct()
    {
        $this->model = new IcafeCnkModel();
        $this->initialize();
    }

    /**
     * 初始化
     */
    public function initialize()
    {
    }

    /**
     * 获取：净网先锋
     *
     * @param int $icafe_id
     * @return array
     */
    public function getCnk(int $icafe_id): array
    {
        return $this->model->where('icafe_id', $icafe_id)->findOrEmpty()->toArray();
    }

    /**
     * 保存：净网先锋
     *
     * @param int   $icafe_id
     * @param array $data
     * @return bool
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function saveCnk(int $icafe_id, array $data): bool
    {
        $cnk = $this->model->where('icafe_id', $icafe_id)->find();
        if (!$cnk) {
            $cnk = new $this->model;
        }

        return $cnk->save($data);
    }
}